package amq.producers;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnectionFactory;

public class TestProducer {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		if(args.length<2)
		{
			System.out.printf("\t[Info] Please give: Argu1=Connector URI! (ex 'tcp://localhost:61616');" +
					          "Argu2=Message to send\n");
			System.out.printf("\t[Info] Message 'stop' to stop the consumer.\n");
			return;
		}
		ConnectionFactory connectionFactory;  
        Connection connection=null;  
        Session session=null;  
        Destination destination;  
        MessageProducer producer=null;  
        Message message;  
        boolean useTransaction = false;  
        try {  
            connectionFactory = new ActiveMQConnectionFactory(args[0]);  
            connection = connectionFactory.createConnection();  
            connection.start();  
            session = connection.createSession(useTransaction,  
                    Session.AUTO_ACKNOWLEDGE);  
            destination = session.createQueue("TEST.QUEUE");  
            producer = session.createProducer(destination);  
            message = session.createTextMessage(args[1]);              
            producer.send(message);  
            System.out.printf("\t[Info] Message sent!\n");
        } catch (JMSException jmsEx) {  
            jmsEx.printStackTrace();  
        } finally {  
            producer.close();  
            session.close();  
            connection.close();  
        }  

	}

}
